package cn.zzf.core;

import cn.zzf.mapper.ClassroomInfoMapper;
import cn.zzf.po.ClassroomInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author GaoFeng2017
 * @date 2018-02-23 14:49:25
 **/

public class ClassroomWriteToDb {
    private String building;
    private Byte campus;
    private List<String> rooms;

    public ClassroomWriteToDb(String building, Byte campus, List<String> rooms) {
        this.building = building;
        this.campus = campus;
        this.rooms = rooms;
    }

    public void writeAll() {
        try {
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
            SqlSession sqlSession = sessionFactory.openSession();
            ClassroomInfoMapper classroomInfoMapper = sqlSession.getMapper(ClassroomInfoMapper.class);
            for (String room :rooms) {
                ClassroomInfo classroomInfo = new ClassroomInfo();
                classroomInfo.setBuilding(this.building);
                classroomInfo.setCampus(this.campus);
                classroomInfo.setGmtCreate(new Date());
                classroomInfo.setName(room);
                classroomInfo.setDescription("投影课教室");
                classroomInfoMapper.insertSelective(classroomInfo);
            }
            sqlSession.commit();
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
